import 'package:flutter/widgets.dart';
import '../../../public.dart';

typedef CommentTitleTapCallback = void Function(int type);

/// 评论头部
class ReaderCommentTitle extends StatefulWidget {
  final ComicModel comicModel;
  final int totalCount;
  final bool isReply; // 是否回复模式，此模式不显示回复按钮
  final bool showSwitch; // 是否显示switch按钮
  final CommentTitleTapCallback onTap;
  final bool isComicType;
  final int cliType;

  ReaderCommentTitle(
      {Key key,
      this.comicModel,
      this.totalCount,
      this.onTap,
      this.cliType = 1,
      this.isComicType = false,
      this.isReply = false,
      this.showSwitch = false})
      : super(key: key);

  @override
  ReaderCommentTitleState createState() => new ReaderCommentTitleState();
}

class ReaderCommentTitleState extends State<ReaderCommentTitle> {
  bool enableCom = true;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    int total = widget.totalCount;
    return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      Row(
        children: [
          Text(
            '${widget.isComicType ? '漫画' : '章节'}评论（${total == -1 ? 0 : total}）',
            style: TextStyle(fontSize: 16, color: Colors.white),
          ),
          widget.isReply || !widget.showSwitch
              ? SizedBox.shrink()
              : Switch(
                  value: enableCom,
                  activeColor: Colors.white,
                  inactiveTrackColor: ColorUtil.rgba(255, 255, 255, 0.6),
                  activeTrackColor: ColorUtil.rgba(254, 45, 102, 1),
                  onChanged: (value) {
                    enableCom = value;
                    setState(() {});
                  },
                ),
        ],
      ),
      DefaultTextStyle.merge(
          style: TextStyle(
              color: ColorUtil.rgba(255, 255, 255, 0.6), fontSize: 12),
          child: Row(
            children: [buildSort(1), Text(' | '), buildSort(2)],
          ))
    ]);
  }

  buildSort(int type) {
    TextStyle style = type == widget.cliType
        ? TextStyle(color: ColorUtil.rgba(255, 215, 59, 1))
        : null;
    return GestureDetector(
        onTap: () {
          if (widget.cliType == type) {
            return;
          }
          widget.onTap(type);
          setState(() {});
        },
        child: Text(type == 1 ? '热门' : '最新', style: style));
  }
}
